import { lazyReportBatch } from '../export';

/**
 * 监听LCP
 * 指浏览器从开始加载网页到绘制出页面中最大的内容元素的时间点
 */
const observeLCP = () => {
  const entryHandler = (list) => {
    if (observer) {
      observer.disconnect();
    }
    for (const entry of list.getEntries()) {
      observer.disconnect();
      const json = entry.toJSON();
      console.log(json);
      const reportData = {
        ...json,
        type: 'performance',
        subType: entry.name,
        pageUrl: window.location.href,
        title: '最大内容绘制指标',
      };
      // 发送数据
      lazyReportBatch(reportData);
    }
  };

  const observer = new PerformanceObserver(entryHandler);
  observer.observe({ type: 'largest-contentful-paint', buffered: true });
};
export default observeLCP;
